package top.xxyu.controller;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import top.xxyu.annotation.Log;
import top.xxyu.core.Msg;
import top.xxyu.dao.QuartzJobDao;
import top.xxyu.entity.QuartzJob;
import top.xxyu.quartz.JobUtil;
import top.xxyu.util.BeanUtils;

import java.util.Objects;
import java.util.UUID;

/**
 *
 * 定时任务 controller
 */
@RestController()
@RequestMapping(value = "/job")
public class JobController {

    @Autowired
    QuartzJobDao quartzJobDao;

    @Autowired
    JobUtil jobUtil;


    @ApiOperation(value = "/addJob", httpMethod = "POST", notes = "添加任务类")
    @Log(desc = "添加任务")
    @PostMapping(value = "addJob")
    @ResponseBody
    public Msg addJob(@RequestBody QuartzJob job) {
        job.setStatus(false);
        if (Objects.isNull(job.getId()) && StringUtils.isBlank(job.getId())) {
            job.setId(UUID.randomUUID().toString().replace("-",""));
        }
        quartzJobDao.save(job);
        Msg msg = new Msg("200", "success pauseJob");
        return msg;
    }


    @Log(desc = "停止任务")
    @PostMapping(value = "pauseJob/{id}")
    @ResponseBody
    public Msg pauseJob(@PathVariable String id) {
            QuartzJob job = quartzJobDao.getOne(id);
            jobUtil.remove(job);
            job.setStatus(false);
            quartzJobDao.save(job);
        return new Msg("200", "success pauseJob");
    }


    @ApiOperation(value = "/updateJob", httpMethod = "POST", notes = "更新任务")
    @Log(desc = "更新任务", type = Log.LOG_TYPE.UPDATE)
    @PostMapping(value = "updateJob")
    @ResponseBody
    public Msg updateJob(QuartzJob job) {
      QuartzJob oldJob = quartzJobDao.getOne(job.getId());
      BeanUtils.copyPropertiesIgnoreNull(job, oldJob);

        if (jobUtil.checkJob(job)) {
            return new Msg("201", "已经启动任务无法更新,请停止后更新");
        }
        quartzJobDao.save(job);
        return new Msg("200", "success pauseJob");
    }

    @ApiOperation(value="获取所有计划任务", notes="")
    @RequestMapping(value = "/{page}/{size}", method = RequestMethod.GET)
    public Msg getTaskAll(@PathVariable int page, @PathVariable int size) {
        Msg msg = new Msg();
        Sort orders = new Sort(Sort.Direction.DESC,"id");
        Page<QuartzJob> result = quartzJobDao.findAll(PageRequest.of(page - 1, size, orders));
        msg.setResult(result);
        return msg;
    }






    @Log(desc = "删除任务", type = Log.LOG_TYPE.DEL)
    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除任务")
    @PostMapping(value = "/del/{id}")
    @ResponseBody
    public Msg del(@PathVariable String id) {
        Msg msg = new Msg();
        QuartzJob job = quartzJobDao.getOne(id);
        boolean flag = jobUtil.checkJob(job);
        if ((flag && !job.getStatus()) || !flag && job.getStatus()) {
            msg.setMsg("您任务表状态和web任务状态不一致,无法删除");
            return msg;
        }
        if (flag) {
            msg.setMsg("该任务处于启动中，无法删除");
            return msg;
        }
        quartzJobDao.deleteById(id);
        msg.setCode("200");
        msg.setMsg("任务删除成功");
        return msg;
    }


    @Log(desc = "启动任务")
    @PostMapping(value = "startJob/{id}")
    @ResponseBody
    public Msg startJob(@PathVariable String id) {
        Msg msg = new Msg();
            QuartzJob job = quartzJobDao.getOne(id);
            jobUtil.startJob(job);
            job.setStatus(true);
            quartzJobDao.save(job);
        msg.setMsg("启动成功");
        return msg;
    }

}
